/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gettree;

import java.util.ArrayList;
import java.util.Enumeration;

import org.antlr.runtime.tree.Tree;



/**
 *
 * @author user
 */
public class TreeNode implements javax.swing.tree.TreeNode{

	private TreeNode parent ; 
	private String label;
	private ArrayList<TreeNode> childrens = null;
	private int h = -1  ;
	private int li ; 
	private int oldP =-1;
	

	public int getOldP() {
		return oldP;
	}

	public void setOldP(int oldP) {
		this.oldP = oldP;
	}

	public int getLi() {
		return li;
	}

	public void setLi(int li) {
		this.li = li;
	}

	public int getH (){
		if (h==-1)
			h = getH(this);
		return h;
	}

	public int getH(TreeNode t){
		if (t == null)
			return 0 ;
		int max = 0 ;
		for (int i = 0 ; i< t.getChildCount() ; i++){
			int h = t.getChild(i).getH();
			if (max < h)
				max = h ;
		}
		return 1 + max ;
	}


	@Override
	public javax.swing.tree.TreeNode getChildAt(int childIndex) {
		if ( childrens == null || childIndex < 0 || childIndex > childrens.size())
			return null;
		else 
			return childrens.get(childIndex);

	}

	@Override
	public int getChildCount() {
		if (childrens== null)
			return 0 ; 
		return childrens.size();
	}

	@Override
	public javax.swing.tree.TreeNode getParent() {
		return parent;
	}


	@Override
	public int getIndex(javax.swing.tree.TreeNode node) {
		if (childrens==null)
			return -1;
		return childrens.indexOf(node);
	}

	public TreeNode getChild(int i ) {
		if (childrens==null)
			return null;
		return childrens.get(i);
	}

	@Override
	public boolean getAllowsChildren() {
		return true;
	}

	@Override
	public boolean isLeaf() {
		if (childrens==null || childrens.size()==0)
			return true;
		return false;
	}

	@Override
	public Enumeration children() {
		throw new UnsupportedOperationException("Not supported yet.");
	}


	public void addChild (TreeNode child){
		if (childrens==null)
			childrens = new ArrayList<TreeNode>();
		childrens.add(child);
		child.setParent(this);
	}

	public void addChild (TreeNode child, int index){
		if (childrens==null)
			childrens = new ArrayList<TreeNode>();
		child.setParent(this);
		childrens.add(index, child);
	}

	public ArrayList<TreeNode> getChildrens() {
		return childrens;
	}

	public void setChildrens(ArrayList<TreeNode> childrens) {
		if (this.childrens!=null)
			this.childrens.clear();
		else
			this.childrens = new ArrayList<TreeNode>();
		if (childrens!=null)
			for (TreeNode child : childrens){
				addChild(child);
			}
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public void setParent(TreeNode parent) {
		this.parent = parent;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final TreeNode other = (TreeNode) obj;
		if ((this.label == null) ? (other.label != null) : !this.label.equals(other.label)) {
			return false;
		}
		if (this.childrens != other.childrens && (this.childrens == null || !this.childrens.equals(other.childrens))) {
			return false;
		}
		return true;
	}

	@Override
	public int hashCode() {
		int hash = 7;
		hash = 83 * hash + (this.label != null ? this.label.hashCode() : 0);
		hash = 83 * hash + (this.childrens != null ? this.childrens.hashCode() : 0);
		return hash;
	}

	public TreeNode(TreeNode parent, String label , ArrayList<TreeNode> childrens) {
		this.parent = parent;
		if(parent!=null)
			this.parent.addChild(this);
		this.label = label;
		setChildrens(childrens);
	}

	@Override
	public String toString() {
		return label ;
	}
	
	public void deleteChAndReplace (TreeNode n ) {
		ArrayList<TreeNode> newCh = new ArrayList<TreeNode>();
		if (childrens==null)
				return;
		for (int i = 0 ; i < childrens.size() ; i++){
			if (childrens.get(i).getLabel().trim().equals(n.getLabel())){
				for (int j = 0 ; j < n.getChildCount() ; j ++){
					newCh.add(n.getChild(j));
				}
			}
			else
				newCh.add(childrens.get(i));
		}
		//childrens=newCh;
		setChildrens(newCh);
	}
	
	public void deleteChild(int index)
	{
		childrens.remove(index);
	}


}
